package com.be.beadmin.design.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.dto.input.BopDto;
import com.be.beadmin.design.dto.input.BopTotalDto;
import com.be.beadmin.design.dto.output.BopEx;
import com.be.beadmin.design.dto.output.BopInfoEx;
import com.be.beadmin.design.dto.output.BopTotalEx;
import com.be.beadmin.design.entity.BopInfo;
import com.be.beadmin.design.service.BopTotalService;
import com.be.beadmin.project.entity.BatchProduct;
import com.be.beadmin.project.entity.ProductInfo;
import com.be.beadmin.project.service.BatchProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author Jiahao Liu
* @date 2023-02-22
**/
@Api(tags = {"设计管理-工艺路径"})
@RestController
public class BopTotalController {

    @Autowired
    private  BopTotalService bopTotalService;


    @ApiOperation("获取工艺路径工序信息(根据BopId)")
    @PostMapping(value = "/design/getBopTotalById")
    public ResultModel getBopTotalById(@ApiParam(value = "工艺路径id",required = true) @RequestParam String id)  {

        try {
            BopEx bopEx = bopTotalService.getBopTotalById(id);
            return ResultModel.success(bopEx) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }

    @ApiOperation("获取工艺路径分页信息")
    @PostMapping(value = "/design/getBopTotalByPage")
    public ResultModel getBopTotalByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(bopTotalService.getBopTotalByPage(pageEntity)) ;
    }

    @ApiOperation("获取启用的工艺路径名称及编码")
    @PostMapping(value = "/design/getBopCodeAndName")
    public ResultModel getBopCodeAndName()  {
        return ResultModel.success(bopTotalService.getBopCodeAndName()) ;
    }


    @ApiOperation("（可用于判断名称唯一）根据工艺名称获取工艺路径")
    @PostMapping("/design/getBopTotalByBopName")
    public ResultModel getBopTotalByBopName(
            @ApiParam(value = "工艺路径名称",required = true) @RequestParam String bopTotalName){
            return ResultModel.success(bopTotalService.getBopTotalByBopName(bopTotalName));
    }

    @ApiOperation("根据工艺Code获取所有工序")
    @PostMapping("/design/getBopInfoByBopName")
    public ResultModel getBopInfoByBopName(
            @ApiParam(value = "工艺路径名称",required = true) @RequestParam String bopTotalCode){
        return ResultModel.success(bopTotalService.getBopInfoByBopCode(bopTotalCode));
    }

    @ApiOperation("新增工艺路径")
    @PostMapping("/design/addBop")
    public ResultModel addBop(@RequestBody BopDto bopDto){
        try {
            bopTotalService.addBop(bopDto);
            return ResultModel.success(ResultModel.ADD);
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }
    @ApiOperation("删除工艺路径")
    @PostMapping("/design/delBopTotal")
    public ResultModel delBopTotal(@RequestBody BopTotalDto bopTotalDto){
        bopTotalService.delBopTotal(bopTotalDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

//    @ApiOperation("编辑工艺路径")
//    @PostMapping("/design/updateBopTotal")
//    public ResultModel updateBopTotal(@RequestBody BopTotalDto bopTotalDto){
//        bopTotalService.updateBopTotal(bopTotalDto);
//        return ResultModel.success(ResultModel.UPDATE);
//    }

    @ApiOperation("编辑工艺路径")
    @PostMapping("/design/updateBop")
    public ResultModel updateBop(@RequestBody BopDto bopDto){
        bopTotalService.updateBop(bopDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("导出")
    @PostMapping("/design/exportBopTotal")
    public void exportBopTotal(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<BopTotalEx> bopTotalPage = bopTotalService.getBopTotalByPage(pageEntity);
        List<BopTotalEx> bopTotalExes = bopTotalPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("BopTotal接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), BopTotalEx.class).sheet("sheet1").doWrite(bopTotalExes);
    }
}